#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File ：AddressController.py
@Author ：zqy
@Email : zqingy@work@163.com 
@note: 
"""
from flask import g
from flask_restx import Namespace, Resource

from application.Parser.AddressParser import AddressValidator
from application.controller import register_crud_model
from application.logic.AddressLogic import AddressLogic
from application.util.ResponseUtil import ResponseUtil

address_ns = Namespace('address', description='地址模块')
address_parser: AddressValidator = AddressValidator
register_crud_model(
    ns=address_ns,
    logic_cls=AddressLogic,
    parser=address_parser,
    route="",
    id_type="int",
    roles=["user"],
    include_operations=["put", "post", "delete"]
)


@address_ns.route("")
class GetAddress(Resource):
    def get(self):
        """获取地址"""
        return ResponseUtil(data=AddressLogic.get_by_user_id(g.user.id)).success()


@address_ns.route("/<int:_id>/setDefault")
class SetDefault(Resource):
    def put(self, _id):
        """设置默认地址"""
        return ResponseUtil(data=AddressLogic.set_default_address(_id)).success()
